<template lang="pug">
  div.kalix-article
    keep-alive
      kalix-tree-grid.duty-wrapper(bizKey="departments" title="院系录入"
      style="padding-top:0"
      ref="kalixTreeGrid"
      v-bind:targetURL="treeUrl"
      v-bind:isToolBarSelf="true"
      v-bind:toolbarBtnList="treeToolbarBtnList"
      v-bind:onToolBarSelfClick="onToolBarClick"
      v-bind:bizDialog="bizDialog"
      v-bind:columns='columns'
      v-on:selectedRow="getSelectRow"
      v-bind:isRowButtonSelf="true"
      v-bind:jsonStr="jsonStr"
      v-bind:noSearchParam:sync="noSearchParam"
      v-bind:isColumnfixed="false"
      v-bind:btnList="btnList"
      )
</template>

<script type="text/ecmascript-6">
  import FormModel from './model'
  import {DepartmentsURL} from '../../config.toml'
  import {departmentsConfigBtnList} from './config'
  import KalixTable from '../../../../../components/corelib/components/common/baseTable'
  import Message from '../../../../../components/corelib/common/message'
  import kalixTreeGrid from '../../../../../components/forum/treeGrid'

  export default {
    name: 'kalix-training-departmentslist',
    components: {KalixTable, kalixTreeGrid},
    data() {
      return {
        btnList: departmentsConfigBtnList,
        departmentsURL: DepartmentsURL,
        noSearchParam: true,
        // tableFields: [
        //   {prop: 'name', label: '院系名称'},
        //   {prop: 'collegeLevel', label: '院系级别'}
        // ],
        // departmentsDialog: [
        //   {id: 'add', dialog: 'DepartmentsAdd'},
        //   {id: 'edit', dialog: 'DepartmentsEdit'},
        //   {id: 'delete', dialog: 'DepartmentsDelete'},
        //   {id: 'view', dialog: 'DepartmentsView'}
        // ],
        departmentsBtnList: departmentsConfigBtnList,
        treeToolbarBtnList: [
          {id: 'add', isShow: true, title: '添加', icon: 'icon-pinleizengjia', isPermission: true},
          {id: 'delete', isShow: true, icon: 'icon-shanchu', title: '删除', isPermission: true},
          {id: 'edit', isShow: true, icon: 'icon-bianji', title: '编辑', isPermission: true}
        ],
        treeUrl: DepartmentsURL + '/getAllDepByParentId?parentId=-1',
        addFormModel: Object.assign({}, FormModel),
        editFormModel: Object.assign({}, FormModel),
        auditingFormModel: Object.assign({}, FormModel),
        parentId: undefined,
        kalixDialog: undefined,
        currentRow: undefined,
        isIconSelf: true,
        jsonStr: '',
        bizDialog: [
          {id: 'add', dialog: 'departmentsAdd'},
          {id: 'edit', dialog: 'departmentsEdit'}
        ],
        optActions: {
          title: '操作',
          type: 'action',
          actions: [{
            type: 'edit',
            text: '编辑',
            icon: 'el-icon-edit'
          }, {
            type: 'delete',
            text: '删除',
            icon: 'el-icon-delete'
          }],
          width: '150'
        },
        columns: [{
          type: 'hidden',
          key: 'id',
          width: '0'
        }, {
          type: 'hidden',
          key: 'parentId',
          width: '0'
        }, {
          title: '院系名称',
          key: 'content',
          width: '120'
        }]
      }
    },
    methods: {
      onToolBarClick(btnId) {
        if (btnId === 'add') {
          console.log('addBtnId==========')
          this.onAddClick()
        }
        if (btnId === 'edit') {
          console.log('edit==========')
          this.onEditClick()
        }
        if (btnId === 'delete') {
          console.log('delete==========')
          this.onDeleteClick()
        }
      },
      onRefreshClick() {
        if (this.treeUrl !== undefined) {
          this.$refs.kalixTreeGrid.getData()
        }
      },
      onAddClick() { // 添加按钮点击事件
        if (this.currentRow === undefined) {
          let that = this
          this.$refs.kalixTreeGrid.getKalixDialog('add', (_kalixDialog) => {
            this.kalixDialog = _kalixDialog
            setTimeout(() => {
              this.addFormModel.parentId = '-1'
              this.addFormModel.parentContent = '根目录'
              this.addFormModel.isLeaf = '0'
              this.kalixDialog.$refs.kalixBizDialog.open('添加', false, this.addFormModel)
              if (typeof (that.kalixDialog.init) === 'function') {
                console.log('this.dialogOptions============', this.dialogOptions)
                that.kalixDialog.init(this.dialogOptions) //  需要传参数，就在dialog里面定义init方法
              }
            }, 20)
          })
          return
        }
        let that = this
        this.$refs.kalixTreeGrid.getKalixDialog('add', (_kalixDialog) => {
          this.kalixDialog = _kalixDialog
          setTimeout(() => {
            this.addFormModel.parentId = this.currentRow.id
            this.addFormModel.parentContent = this.currentRow.content
            this.addFormModel.isLeaf = '0'
            this.kalixDialog.$refs.kalixBizDialog.open('添加', false, this.addFormModel)
            if (typeof (that.kalixDialog.init) === 'function') {
              console.log('this.dialogOptions============', this.dialogOptions)
              that.kalixDialog.init(this.dialogOptions) //  需要传参数，就在dialog里面定义init方法
            }
          }, 20)
        })
      },
      onEditClick() { // 修改按钮点击事件
        if (this.currentRow === undefined) {
          Message.error('请选择要操作的记录！')
          return
        }
        let that = this
        this.$refs.kalixTreeGrid.getKalixDialog('edit', (_kalixDialog) => {
          this.kalixDialog = _kalixDialog
          setTimeout(() => {
            this.editFormModel = this.currentRow
            this.editFormModel.parentContent = this.editFormModel.content
            if (this.currentRow.dataPermission !== true) {
              this.currentRow.dataPermission = false
            }
            this.editFormModel.dataPermission = this.currentRow.dataPermission + ''
            console.log('this.editFormModel==============', this.editFormModel)
            this.kalixDialog.$refs.kalixBizDialog.open('编辑', true, this.editFormModel)
            if (typeof (that.kalixDialog.init) === 'function') {
              that.kalixDialog.init(this.dialogOptions) // 需要传参数，就在dialog里面定义init方法
            }
          }, 20)
        })
      },
      onDeleteClick() { // 删除按钮点击事件
        if (this.currentRow === undefined) {
          Message.error('请选择要操作的记录！')
          return
        }
        this.$confirm('确定要删除此院系以及诊断下的所有院系吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return this.axios.request({
            method: 'DELETE',
            url: DepartmentsURL + '/' + this.currentRow.id,
            params: {},
            data: {
              id: this.currentRow.id
            }
          })
        }).then(response => {
          this.$refs.kalixTreeGrid.getData()
          Message.success(response.data.msg)
        }).catch(() => {
        })
      },
      getSelectRow(val) {
        console.log('getSelectRow========', val)
        this.currentRow = val
      }
    }
  }
</script>

<style scoped lang="stylus">
  @import "../../../../../assets/stylus/baseTable.styl"
  .kalix-article
    position relative
    height 100%
    overflow hidden
    box-sizing border-box

    .duty-row
      height 100%
      .duty-col
        height 100%
        box-sizing border-box

    .duty-wrapper
      margin 9px 0
      .kalix-wrapper
        bottom 0 !important

      label
        overflow hidden
        height 28px
        text-overflow ellipsis
        white-space nowrap
</style>
